let fs;
let path;
let exec;
try {
    fs = require("fs");
    path = require("path");
    exec = require("child_process").exec;
} catch (error) {}

function readFileList(dir, filter, pathKeys, filesList, state, negation) {
    const files = fs.readdirSync(dir);
    files.forEach((item, index) => {
        let fullPath = path.join(dir, item);
        const stat = fs.statSync(fullPath);
        if (stat.isDirectory() && state == 1) {
            readFileList(
                path.join(dir, item),
                filter,
                pathKeys,
                filesList,
                state,
                negation
            ); //递归读取文件
        } else {
            if (filter.length > 0) {
                for (let i = 0; i < filter.length; i++) {
                    if (fullPath.indexOf(filter[i]) > -1) {
                        if (pathKeys.length > 0) {
                            let flags = [];
                            for (let j = 0; j < pathKeys.length; j++) {
                                if (negation) {
                                    //取反
                                    if (
                                        pathKeys[j] &&
                                        fullPath.indexOf(pathKeys[j]) == -1
                                    ) {
                                        flags.push(true);
                                    } else {
                                        flags.push(false);
                                    }
                                } else {
                                    if (
                                        pathKeys[j] &&
                                        fullPath.indexOf(pathKeys[j]) > -1
                                    ) {
                                        filesList.push(fullPath);
                                    }
                                }
                            }
                            if (flags.length > 0) {
                                if (
                                    flags.findIndex((item) => item == false) ==
                                    -1
                                ) {
                                    filesList.push(fullPath);
                                }
                            }
                        } else {
                            filesList.push(fullPath);
                        }
                        break;
                    }
                }
            }
        }
    });
    return filesList;
}

function getCurrentDate() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    month = month + 1 > 12 ? 1 : month + 1;
    month = month > 9 ? month : "0" + month.toString();
    var day = d.getDate();
    var hour = d.getHours();
    hour = hour > 9 ? hour : "0" + hour.toString();
    var minute = d.getMinutes();
    minute = minute > 9 ? minute : "0" + minute.toString();
    var second = d.getSeconds();
    return `${hour}:${minute}:${second}`;
}
try {
    module.exports = {
        readFileList: readFileList,
        getCurrentDate: getCurrentDate,
    };
} catch (error) {}
